import { privacyChannel } from "../Script/privacyChannel";
import { random, shuffle, uniqueArr } from "../Utils";
import DataManager, { DATA_GAME, DATA_LEVEL, DATA_LEVEL_DIFFICULTY } from "../manager/DataManager";
import PoolManager from "../manager/PoolManager";
import Bottle from "./Bottle";

// carolsail
const { ccclass, property } = cc._decorator;

@ccclass
export default class Level extends cc.Component {

    bottles: cc.Node = null
    complete: number = 0

    init() {
        // this.bottles = this.node.getChildByName('bottles')
        // for (let i = 0; i < this.bottles.children.length; i++) {
        //     const bottle = this.bottles.children[i]
        //     const bottleComp = bottle.getComponent(Bottle)
        //     bottleComp.init(i)
        // }
        this.bottles = this.node.getChildByName('bottles')
        this.bottles.removeAllChildren()
        let bottles = 14, empty = 2, locked = 1, data = []

        if (DataManager.instance.stageModel) {
            const levelInfo = DATA_LEVEL_DIFFICULTY[0]
            bottles = levelInfo.bottles
            empty = levelInfo.empty
            locked = levelInfo.locked
            data = JSON.parse(JSON.stringify(levelInfo.data))

        } else {
            if (DATA_LEVEL[DataManager.instance.level - 1]) {
                const levelInfo = DATA_LEVEL[DataManager.instance.level - 1]
                bottles = levelInfo.bottles
                empty = levelInfo.empty
                locked = levelInfo.locked
                data = JSON.parse(JSON.stringify(levelInfo.data))
            }
        }


        // if (DATA_LEVEL[DataManager.instance.level - 1]) {
        //     const levelInfo = DATA_LEVEL[DataManager.instance.level - 1]
        //     bottles = levelInfo.bottles
        //     empty = levelInfo.empty
        //     locked = levelInfo.locked
        //     data = JSON.parse(JSON.stringify(levelInfo.data))
        // }
        const { nums: waterNums, colors: waterColors } = DATA_GAME
        let colorIndexGroup: number[][] = []
        // 是否携带了初始化数据
        if (data.length) {
            colorIndexGroup = data
        } else {
            const createGroupData = () => {
                // 所有颜色
                let colorIndexArr: number[] = []
                for (let i = 0; i < bottles - empty; i++) {
                    const colorIndex = random(0, waterColors.length - 1)
                    for (let j = 0; j < waterNums; j++) colorIndexArr.push(colorIndex)
                }
                colorIndexArr = shuffle(colorIndexArr)
                // 分组
                let colorGroupArr: number[][] = []
                for (let i = 0; i < bottles - empty; i++) {
                    let colorGroup: number[] = []
                    for (let index = 0; index < waterNums; index++) {
                        colorGroup.push(colorIndexArr.pop())
                    }
                    colorGroupArr.push(colorGroup)
                }
                // 检查彻底打散，是否存在满水情况
                let isFull = false
                for (let i = 0; i < colorGroupArr.length; i++) {
                    const temp = uniqueArr(colorGroupArr[i])
                    if (temp.length <= 1) {
                        isFull = true
                        break
                    }
                }
                if (isFull) {
                    // console.log('满水情况，重新生成')
                    return createGroupData()
                }
                return colorGroupArr
            }
            colorIndexGroup = createGroupData()
            // console.log(colorIndexGroup)
        }
        // 加入空瓶
        for (let i = 0; i < empty; i++) {
            colorIndexGroup.push([])
        }
        // 加入锁信息
        let colorIndexObj: { data: number[], isLocked: boolean }[] = []
        let lockNums = locked
        for (let i = 0; i < colorIndexGroup.length; i++) {
            const arr = colorIndexGroup[i]
            const colorObj = { data: arr, isLocked: false }
            if (arr.length <= 0) {
                if (lockNums > 0) {
                    lockNums -= 1
                    colorObj.isLocked = true
                }
            }
            colorIndexObj.push(colorObj)
        }
        // 打散数据
        if (!data.length) colorIndexObj = shuffle(colorIndexObj)

        // 是否为重置
        if (DataManager.instance.skillReset) {
            DataManager.instance.skillReset = false
            colorIndexObj = DataManager.instance.levelData
        } else {
            DataManager.instance.levelData = colorIndexObj
        }
        // 位置信息
        const bottlePosArr = this.getBottlePos(bottles)
        // 初始化
        let total = 0
        if (!DataManager.instance.stageModel) {//普通模式
            for (let i = 0; i < colorIndexObj.length; i++) {
                const { data, isLocked } = colorIndexObj[i]
                total += data.length
                const bottle = PoolManager.instance.getNode(`Bottle${DataManager.instance.skinIndex}`, this.bottles)
                bottle.setPosition(bottlePosArr[i])
                bottle.getChildByName('locked').active = isLocked
                const bottleComp = bottle.getComponent(Bottle)
                bottleComp.init(i, data)
            }
            this.complete = Math.floor(total / waterNums)
        } else {//挑战模式

            // for (let i = colorIndexObj.length - 1; i >= 0; i--) {
            //     const { data, isLocked } = colorIndexObj[i]
            //     total += data.length
            //     const bottle = PoolManager.instance.getNode(`Bottle${DataManager.instance.skinIndex}`, this.bottles)
            //     // bottle.setPosition(bottlePosArr[i])
            //     bottle.getChildByName('locked').active = isLocked
            //     const bottleComp = bottle.getComponent(Bottle)
            //     bottle.scale = 0.6
            //     bottleComp.init(i, data)
            //     bottle.setPosition(bottlePosArr[i].x, bottlePosArr[i].y + 1000, 0)
            //     cc.tween(bottle).to(2 * i, { position: bottlePosArr[i] }).start()


            // }
            // this.complete = Math.floor(total / waterNums)
            for (let i = 0; i < colorIndexObj.length; i++) {
                const { data, isLocked } = colorIndexObj[i]
                total += data.length
                const bottle = PoolManager.instance.getNode(`Bottle${DataManager.instance.skinIndex}`, this.bottles)

                bottle.setPosition(bottlePosArr[colorIndexObj.length - i - 1])
                bottle.getChildByName('locked').active = isLocked
                const bottleComp = bottle.getComponent(Bottle)
                bottle.scale = 0.6
                bottleComp.init(i, data)
                //bottle.active = false
                bottle.setPosition(bottlePosArr[colorIndexObj.length - 1 - i].x, bottlePosArr[colorIndexObj.length - 1 - i].y + 1000, 0)
                cc.tween(bottle).delay(i * 0.015).to(0.5, { position: bottlePosArr[colorIndexObj.length - i - 1] }).start()


            }
            this.complete = Math.floor(total / waterNums)

        }





    }

    getBottlePos(nums: number) {
        let posArr = []
        switch (nums) {
            case 2:
                posArr = [cc.v2(-115, 0), cc.v2(115, 0)]
                break
            case 3:
                posArr = [cc.v2(-230, 0), cc.v2(0, 0), cc.v2(230, 0)]
                break
            case 4:
                posArr = [cc.v2(-270, 0), cc.v2(-90, 0), cc.v2(90, 0), cc.v2(270, 0)]
                break
            case 5:
                posArr = [cc.v2(-230, -50), cc.v2(0, -50), cc.v2(230, -50), cc.v2(-115, 300), cc.v2(115, 300)]
                break
            case 6:
                posArr = [cc.v2(-230, -50), cc.v2(0, -50), cc.v2(230, -50), cc.v2(-230, 300), cc.v2(0, 300), cc.v2(230, 300)]
                break
            case 7:
                posArr = [cc.v2(-270, -50), cc.v2(-90, -50), cc.v2(90, -50), cc.v2(270, -50), cc.v2(-230, 300), cc.v2(0, 300), cc.v2(230, 300)]
                break
            case 8:
                posArr = [cc.v2(-270, -50), cc.v2(-90, -50), cc.v2(90, -50), cc.v2(270, -50), cc.v2(-270, 300), cc.v2(-90, 300), cc.v2(90, 300), cc.v2(270, 300)]
                break
            case 9:
                posArr = [cc.v2(-260, -50), cc.v2(-130, -50), cc.v2(0, -50), cc.v2(130, -50), cc.v2(260, -50), cc.v2(-270, 300), cc.v2(-90, 300), cc.v2(90, 300), cc.v2(270, 300)]
                break
            case 10:
                posArr = [cc.v2(-260, -50), cc.v2(-130, -50), cc.v2(0, -50), cc.v2(130, -50), cc.v2(260, -50), cc.v2(-260, 300), cc.v2(-130, 300), cc.v2(0, 300), cc.v2(130, 300), cc.v2(260, 300)]
                break
            case 11:
                posArr = [cc.v2(-287.5, -50), cc.v2(-172.5, -50), cc.v2(-57.5, -50), cc.v2(57.5, -50), cc.v2(172.5, -50), cc.v2(287.5, -50), cc.v2(-260, 300), cc.v2(-130, 300), cc.v2(0, 300), cc.v2(130, 300), cc.v2(260, 300)]
                break
            case 12:
                posArr = [cc.v2(-287.5, -50), cc.v2(-172.5, -50), cc.v2(-57.5, -50), cc.v2(57.5, -50), cc.v2(172.5, -50), cc.v2(287.5, -50), cc.v2(-287.5, 300), cc.v2(-172.5, 300), cc.v2(-57.5, 300), cc.v2(57.5, 300), cc.v2(172.5, 300), cc.v2(287.5, 300)]
                break
            case 13:
                posArr = [cc.v2(-315, -50), cc.v2(-210, -50), cc.v2(-105, -50), cc.v2(0, -50), cc.v2(105, -50), cc.v2(210, -50), cc.v2(315, -50), cc.v2(-287.5, 300), cc.v2(-172.5, 300), cc.v2(-57.5, 300), cc.v2(57.5, 300), cc.v2(172.5, 300), cc.v2(287.5, 300)]
                break
            case 14:
                posArr = [cc.v2(-315, -50), cc.v2(-210, -50), cc.v2(-105, -50), cc.v2(0, -50), cc.v2(105, -50), cc.v2(210, -50), cc.v2(315, -50), cc.v2(-315, 300), cc.v2(-210, 300), cc.v2(-105, 300), cc.v2(0, 300), cc.v2(105, 300), cc.v2(210, 300), cc.v2(315, 300)]
                break
            case 54:
                let startY = 375
                let startX = -315
                let SpaceX = 78.75
                let SpaceY = 170
                posArr = [cc.v2(startX + SpaceX * 0, startY), cc.v2(startX + SpaceX * 1, startY), cc.v2(startX + SpaceX * 2, startY), cc.v2(startX + SpaceX * 3, startY), cc.v2(startX + SpaceX * 4, startY),
                cc.v2(startX + SpaceX * 5, startY), cc.v2(startX + SpaceX * 6, startY), cc.v2(startX + SpaceX * 7, startY), cc.v2(startX + SpaceX * 8, startY),


                cc.v2(startX + SpaceX * 0, startY - SpaceY * 1), cc.v2(startX + SpaceX * 1, startY - SpaceY * 1), cc.v2(startX + SpaceX * 2, startY - SpaceY * 1), cc.v2(startX + SpaceX * 3, startY - SpaceY * 1), cc.v2(startX + SpaceX * 4, startY - SpaceY * 1),
                cc.v2(startX + SpaceX * 5, startY - SpaceY * 1), cc.v2(startX + SpaceX * 6, startY - SpaceY * 1), cc.v2(startX + SpaceX * 7, startY - SpaceY * 1), cc.v2(startX + SpaceX * 8, startY - SpaceY * 1),


                cc.v2(startX + SpaceX * 0, startY - SpaceY * 2), cc.v2(startX + SpaceX * 1, startY - SpaceY * 2), cc.v2(startX + SpaceX * 2, startY - SpaceY * 2), cc.v2(startX + SpaceX * 3, startY - SpaceY * 2), cc.v2(startX + SpaceX * 4, startY - SpaceY * 2),
                cc.v2(startX + SpaceX * 5, startY - SpaceY * 2), cc.v2(startX + SpaceX * 6, startY - SpaceY * 2), cc.v2(startX + SpaceX * 7, startY - SpaceY * 2), cc.v2(startX + SpaceX * 8, startY - SpaceY * 2),

                cc.v2(startX + SpaceX * 0, startY - SpaceY * 3), cc.v2(startX + SpaceX * 1, startY - SpaceY * 3), cc.v2(startX + SpaceX * 2, startY - SpaceY * 3), cc.v2(startX + SpaceX * 3, startY - SpaceY * 3), cc.v2(startX + SpaceX * 4, startY - SpaceY * 3),
                cc.v2(startX + SpaceX * 5, startY - SpaceY * 3), cc.v2(startX + SpaceX * 6, startY - SpaceY * 3), cc.v2(startX + SpaceX * 7, startY - SpaceY * 3), cc.v2(startX + SpaceX * 8, startY - SpaceY * 3),

                cc.v2(startX + SpaceX * 0, startY - SpaceY * 4), cc.v2(startX + SpaceX * 1, startY - SpaceY * 4), cc.v2(startX + SpaceX * 2, startY - SpaceY * 4), cc.v2(startX + SpaceX * 3, startY - SpaceY * 4), cc.v2(startX + SpaceX * 4, startY - SpaceY * 4),
                cc.v2(startX + SpaceX * 5, startY - SpaceY * 4), cc.v2(startX + SpaceX * 6, startY - SpaceY * 4), cc.v2(startX + SpaceX * 7, startY - SpaceY * 4), cc.v2(startX + SpaceX * 8, startY - SpaceY * 4),

                cc.v2(startX + SpaceX * 0, startY - SpaceY * 5), cc.v2(startX + SpaceX * 1, startY - SpaceY * 5), cc.v2(startX + SpaceX * 2, startY - SpaceY * 5), cc.v2(startX + SpaceX * 3, startY - SpaceY * 5), cc.v2(startX + SpaceX * 4, startY - SpaceY * 5),
                cc.v2(startX + SpaceX * 5, startY - SpaceY * 5), cc.v2(startX + SpaceX * 6, startY - SpaceY * 5), cc.v2(startX + SpaceX * 7, startY - SpaceY * 5), cc.v2(startX + SpaceX * 8, startY - SpaceY * 5),


                ]
                break
        }
        return posArr
    }
}
